package com.jeecms.wicool.dao.car.impl;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.CarTypeDao;
import com.jeecms.wicool.entity.car.CarImg;
import com.jeecms.wicool.entity.car.CarType;

@Repository
public class CarTypeDaoImpl extends HibernateBaseDao<CarType, Integer>
		implements CarTypeDao {

	@Override
	protected Class<CarType> getEntityClass() {
		// TODO Auto-generated method stub
		return CarType.class;
	}
	@Override
	public Pagination getPage(String typeName,Integer carBrandId,Integer carSeriesId,Integer year ,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from CarType bean where 1=1 ");
			if (!StringUtils.isBlank(typeName)){
				f.append(" and bean.typeName like :typeName");
				f.setParam("typeName", "%" + typeName + "%");
			}if (null!=carBrandId){
				f.append(" and bean.carBrand.id = :carBrandId");
				f.setParam("carBrandId",carBrandId);
			}if (null!=carSeriesId){
				f.append(" and bean.carSeries.id = :carSeriesId");
				f.setParam("carSeriesId",carSeriesId);
			}if (null!=year){
				f.append(" and bean.year = :year");
				f.setParam("year",year);
			}if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			f.append(" order by bean.orderNum desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public CarType findById(Integer id) {
		CarType entity = get(id);
		return entity;
	}
	@Override
	public CarType save(CarType bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public CarType deleteById(Integer id) {
		CarType entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
			getSession().flush();
		}
		return entity;
	}
	@Override
	public List<CarType> getAll(CmsUser user) {
		Finder f= Finder.create("select bean from CarType bean where 1=1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
	@Override
	public List<CarType> getCarTypeByCarSeries(Integer carSeriesId,CmsUser user){
		Finder f = Finder.create("from CarType bean where 1=1 ");
		if(carSeriesId!=null){
			f.append(" and bean.carSeries.id = :carSeriesId") ;
			f.setParam("carSeriesId",carSeriesId);
		}
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
}